const express = require('express')
const router = express.Router()
const { requests, maoyan, Webmovie} = require("../netword/axios")
const { hotList } = require("../mock/mock")

async function Obtain(params, req) {
  try {
    let data = await requests({
      url: "/api",
      params
    })
    req.send(data.data)
  } catch (error) {
    req.send(error)
  }

}


// 获取轮播图数据
router.get("/banner", async (res, req, next)=>{
  // 参数
  let body = {pageType: 1, adSource: 2}

  try {
    let data = await requests({
      url: "/api",
      params: {
        appid: "movie", // 京东后端自动分配的
        functionId: "getCmsInfoByPageType", // 固定写死的
        body
      }
    })
    // 返回数据
    req.send(data.data)
  } catch (error) {
    // 返回错误数据
    req.send(error)
  }
})


// 正在热映和即将上映 数据
router.get("/m_channel", async (res, req, next)=>{
  let cityId = res.query.cityId || 11 // 城市id
  let playType = res.query.playType || 2 // 2 表示正在热映  1即将上映
  // 分页
  let pageNum = res.query.pageNum || 1
  let pageSize = res.query.pageSize || 20

  try {
    let data = await Webmovie({
      url: "/m_channel",
      params: {
        cityId,
        playType,
        orderType: 1, // PC 端的排序
        pageNum,
        pageSize
      }
    })
    req.send(data.data)
    
  } catch (error) {
    req.send(error)
  }
})


// 影院信息
router.get("/cinemaInfo", async (res, req, next)=>{
  queryDate = + new Date()
  let cityId = res.query.cityId || 11
  let pageNum = res.query.pageNum || 1
  let pageSize = res.query.pageSize || 10000
  let regionId = res.query.regionId
  let brandId = res.query.brandId
  
  let body = {
    cityId,
    pageSize,
    pageNum,
    queryDate,
    regionId,
    brandId
  }
  try {
    let data = await requests({
      url: "/api",
      params: {
        appid: "movie",
        functionId: "homePageCinema",
        body
      }
    })
    req.send(data.data)
    
  } catch (error) {
    req.send(error)
  }
})


// 影院筛选信息, 城市\品牌
router.get("/filtercinemas", async (res, req, next)=>{
  let cityId = res.query.cityId || 11

  let params = {
    appid: "movie",
    functionId: "filterCinemas",
    body: {
      cityId,
      indexFlag: true
    }
  }

  Obtain(params, req)

})


// 电影详情
router.get("/getMovieDetail", async (res, req, next)=>{
  let movieId = res.query.movieId || 145975 // 电影id
  let cityId = res.query.cityId || 11 // 城市id

  let params = {
    appid: "movie",
    functionId: "getMovieDetailVO",
    body: {
      movieId,
      cityId
    }
  }
  Obtain(params, req)
})


// 所有评论
router.get("/getcomments", async (res, req, next)=>{
  let sku = res.query.jSkuId || 200146224929
  let type = res.query.type || 0
  let offset = res.query.offset || 1
  let num = res.query.num || 10
  let t = + new Date()

  let params = {
    appid: "Apolloec_Order",
    functionId: "komento_soa_getcomments",
    t,
    body: {
      offset,
      type,
      sku,
      num
    }
  }
  Obtain(params, req)
})


// 影院中的电影列表
router.get("/getCinemaMovie", async (res, req, next)=>{
  let cityId = res.query.type || 11
  let cinemaId = res.query.cinemaId || 47985
  let params = {
    functionId: "getTimeCinemaMovieVO",
    appid: "movie",
    body: {
      cinemaId, 
      cityId
    }
  }
  
  Obtain(params, req)
})

// 影院中电影的可放映日期
router.get("/getShowDate", async (res, req, next)=>{
  let movieId = res.query.movieId || 147364
  let cinemaId = res.query.cinemaId || 47985

  let params = {
    appid: "movie",
    functionId: "getMovieIssueShowDate",
    body: {
      cinemaId,
      movieId
    }
  }
  // 运行
  Obtain(params, req)
})

// 电影场次
router.get("/getSession", async (res, req, next)=>{
  let movieId = res.query.movieId || 147364
  let cinemaId = res.query.cinemaId || 47985
  let cityId = res.query.cityId || 11
  let date = res.query.date || '2021-06-17'

  let params = {
    appid: "movie",
    functionId: "getTimeIssuePlanVO",
    body: {
      cinemaId,
      movieId,
      cityId,
      date
    }
  }

  // 运行
  Obtain(params, req)
})

// 所有城市数据
router.get("/getAllCityList", async (res, req, next)=>{

  let params = {
    appid: "movie",
    functionId: "getAllCityList",
  }
  // 运行
  Obtain(params, req)
})

// 热门城市
router.get("/hotList", async (res, req, next)=>{
  req.send({
    hotList
  })
})

// 预告片
router.get("/getTrailerInfoVO", async (res, req, next)=>{
  let movieId = res.query.movieId || 147364
  let params = {
    appid: "movie",
    functionId: "getTrailerInfoVO",
    body: {
      movieId
    }
  }
  // 运行
  Obtain(params, req)
})


// 票房信息
router.get("/getBoxList", async (res, req, next)=>{

  let date = res.query.date || 1
  let isSplit = res.query.isSplit || true

  try {
    let data = await maoyan({
      url: "/getBoxList",
      params: {
        date,
        isSplit
      }
    })
    req.send(data.data)
  } catch (error) {
    req.send(error)
  }
})




module.exports = router